<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .item {
            width: 100px;
            height: 80px;
            border: 1px solid #000;
            margin: 20px;
        }
    </style>
</head>

<body>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</body>

<script>
    let items = document.querySelectorAll('.item');

    //let为块级作用域，所以可用这样执行
    for (let i = 0; i < items.length; i++) {
        items[i].addEventListener('click', function () {
            items[i].style.background = 'pink';
        })
    }

    // for (var i = 0; i < items.length; i++) {
    //     items[i].addEventListener('click', function () {
    //         items[i].style.background = 'pink';
    //     })
    // }
</script>

</html>